package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.sgb.capital.view.widget.DecimalInputTextWatcherMore;
import com.sgb.kjwl.R;
import com.sgb.kjwl.utils.PointUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PublishServicePriceListAdpaterNew extends RecyclerView.Adapter<PublishServicePriceListAdpaterNew.MViewHodle> {

    private Context mContext;
    private List<Map<String, Object>> datas = new ArrayList<>();
    public PublishServicePriceListAdpaterNew(Context mContext, List<Map<String, Object>> listdata) {
        this.mContext = mContext;
        this.datas = listdata;
    }


    @NonNull
    @NotNull
    @Override
    public MViewHodle onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {

        return new MViewHodle(LayoutInflater.from(mContext).inflate(R.layout.item_publish_service_price, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull PublishServicePriceListAdpaterNew.MViewHodle holder, int position) {
        holder.setData(datas.get(position));
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class MViewHodle extends RecyclerView.ViewHolder {
        public TextView textView;
        public EditText etAddPrice;
        public ImageView ivYijia;
        public LinearLayout ll_yijia;

        public MViewHodle(@NonNull @NotNull View itemView) {
            super(itemView);

            textView = itemView.findViewById(R.id.tv_title);
            etAddPrice = itemView.findViewById(R.id.et_add_price);
            ivYijia = itemView.findViewById(R.id.iv_yijia);
            ll_yijia = itemView.findViewById(R.id.ll_yijia);
            etAddPrice.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    if(!etAddPrice.isEnabled()){
                        return;
                    }
                    if(datas.get(getLayoutPosition()).get("price")!=null&&"-1".equals(datas.get(getLayoutPosition()).get("price").toString())){

                        return;
                    }

                    String data = etAddPrice.getText().toString();
                    datas.get(getLayoutPosition()).put("price", data);
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    if(!etAddPrice.isEnabled()){
                        return;
                    }
                    if(datas.get(getLayoutPosition()).get("price")!=null&&"-1".equals(datas.get(getLayoutPosition()).get("price").toString())){

                        return;
                    }

                    String data = etAddPrice.getText().toString();
                    datas.get(getLayoutPosition()).put("price", data);
                }
            });
            ll_yijia.setOnClickListener(view -> {
                if (datas.get(getLayoutPosition()).get("price") != null && "-1".equals(datas.get(getLayoutPosition()).get("price").toString())) {
                    datas.get(getLayoutPosition()).remove("price");
                } else {
                    datas.get(getLayoutPosition()).put("price", "-1");
                }
                notifyDataSetChanged();
            });
        }


        public void setData(Map<String, Object> stringObjectMap) {
            String title = "";
            if (stringObjectMap.get("type_name_1") != null && !TextUtils.isEmpty(stringObjectMap.get("type_name_1").toString())) {
                title = stringObjectMap.get("type_name_1").toString();
            }
            if (stringObjectMap.get("type_name_2") != null && !TextUtils.isEmpty(stringObjectMap.get("type_name_2").toString())) {
                title = title + " | " + stringObjectMap.get("type_name_2").toString();
            }
            if (stringObjectMap.get("type_name_3") != null && !TextUtils.isEmpty(stringObjectMap.get("type_name_3").toString())) {
                title = title + " | " + stringObjectMap.get("type_name_3").toString();
            }
            if (stringObjectMap.get("type_name_4") != null && !TextUtils.isEmpty(stringObjectMap.get("type_name_4").toString())) {
                title = title + " | " + stringObjectMap.get("type_name_4").toString();
            }
            if (stringObjectMap.get("type_name_5") != null && !TextUtils.isEmpty(stringObjectMap.get("type_name_5").toString())) {
                title = title + " | " + stringObjectMap.get("type_name_5").toString();
            }
            textView.setText(title);

            if (stringObjectMap.get("price") != null&&!TextUtils.isEmpty(stringObjectMap.get("price").toString())) {
                ivYijia.setSelected(stringObjectMap.get("price").toString().equals("-1"));
                etAddPrice.setEnabled(!stringObjectMap.get("price").toString().equals("-1"));
                etAddPrice.setText(stringObjectMap.get("price").toString().equals("-1") ? "" : PointUtils.price(Double.parseDouble(stringObjectMap.get("price").toString())));
            } else {
                ivYijia.setSelected(false);
                etAddPrice.setText("");
                etAddPrice.setEnabled(true);
            }
            etAddPrice.addTextChangedListener(new DecimalInputTextWatcherMore(etAddPrice, 10, 2));
        }
    }
}
